﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyBBSWebApi.Bll;
using MyBBSWebApi.Core;
using MyBBSWebApi.Dal;
using MyBBSWebApi.Models;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace MyBBSWebApi.Controllers
{
    //[Route("[controller]/[action]")]//rest风格
    [Route("[controller]")]//restful风格
    [ApiController]
    public class LoginController : ControllerBase
    {
        [HttpGet]
        public List<Users> GetAll()
        {
            UserBll userBll = new UserBll();
            return userBll.GetAll();
        }

        [HttpGet("{userNo}/{password}")]
        public Users GetLoginRes(string userNo, string password)
        {
            UserBll userBll = new UserBll();
            Users user = userBll.CheckLogin(userNo, password);
            return user;
        }
        [HttpPost]
        public string Insert(string userNo,string userName,int userLevel,string password)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.AddUser(userNo,userName,userLevel,password);
            if (rows > 0) 
            {
                return "插入成功";
            }
            return "插入失败";
        }
        [HttpPut]
        public string Update(int id, string userNo, string userName, int userLevel, string password)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.UpdateUser(id,userNo, userName, userLevel, password);
            if (rows > 0)
            {
                return "更新成功";
            }
            return "更新失败";
        }
        [HttpDelete]
        public string Remove(int id)
        {
            UserDal userDal = new UserDal();
            int rows = userDal.RemoveUser(id);
            if (rows > 0)
            {
                return "删除成功";
            }
            return "删除失败";
        }
    }
}
